package com.zhiyume.service;

import com.zhiyume.dao.api.TPrizeMapper;
import com.zhiyume.dao.entity.TPrize;
import com.zhiyume.dao.entity.TPrizeCriteria;
import com.zhiyume.dao.entity.TPrize;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.service.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: TPrizeService.java, v 0.1 2019-01-24 17:55:31 wang Exp $$
 */

@Service
public class TPrizeService {
    @Autowired
    private TPrizeMapper tPrizeMapper;

    /**
     * 根据Id查询信息
     * @return
     */
    public TPrize findPrizeById(long id){
        TPrizeCriteria example = new TPrizeCriteria();
        TPrizeCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TPrize> prizeList = tPrizeMapper.selectByExample(example);
        if(null != prizeList && prizeList.size() > 0) {
            return prizeList.get(0);
        }

        return new TPrize();
    }


    /**
     * 添加信息
     * @param tPrize     * @return
     */
    public TPrize addPrize(TPrize tPrize) throws Exception{

        long id = GenerateKeyUtil.generationPK();
        tPrize.setId(id);

        tPrizeMapper.insert(tPrize);
        
        return tPrize;
    }
    
    
    /**
     * 修改人员信息
     * @param tPrize     * @return
     */
    public int updatePrize(TPrize tPrize) {

        TPrizeCriteria example = new TPrizeCriteria();
        TPrizeCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tPrize.getId());
        return tPrizeMapper.updateByExample(tPrize,example);
    }
    
     /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int deletePrize(long id) {
        TPrizeCriteria example = new TPrizeCriteria();
        TPrizeCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);

        return tPrizeMapper.deleteByExample(example);
    }


    public List<TPrize> queryPrizeList(TPrize dto, PageReq page) {
        TPrizeCriteria tPrizeCriteria = new TPrizeCriteria();
        TPrizeCriteria.Criteria criteria = tPrizeCriteria.createCriteria();
        tPrizeCriteria.setLimitStart(page.getPage()*page.getPageSize());
        tPrizeCriteria.setLimitEnd(page.getPageSize()*page.getPageSize()+page.getPageSize());

        createCriteria(dto, criteria);
        return tPrizeMapper.selectByExample(tPrizeCriteria);
    }



    public int countPrize(TPrize dto) {
        TPrizeCriteria tPrizeCriteria = new TPrizeCriteria();
        TPrizeCriteria.Criteria criteria = tPrizeCriteria.createCriteria();

        createCriteria(dto, criteria);
        return tPrizeMapper.countByExample(tPrizeCriteria);
    }
    
    private void createCriteria(TPrize dto, TPrizeCriteria.Criteria criteria) {
        if(dto.getId()!=null&&-1!=dto.getId()){
            criteria.andIdEqualTo(dto.getId());
        }
        if(StringUtil.isNotBlank(dto.getTitle())){
            criteria.andTitleLike("%"+dto.getTitle()+"%");
        }
        if(StringUtil.isNotBlank(dto.getpType())){
            criteria.andPTypeLike("%"+dto.getpType()+"%");
        }
        if(StringUtil.isNotBlank(dto.getCovertCode())){
            criteria.andCovertCodeLike("%"+dto.getCovertCode()+"%");
        }
        if(StringUtil.isNotBlank(dto.getMode())){
            criteria.andModeLike("%"+dto.getMode()+"%");
        }
        if(StringUtil.isNotBlank(dto.getDirection())){
            criteria.andDirectionLike("%"+dto.getDirection()+"%");
        }
        if(StringUtil.isNotBlank(dto.getCreateTime())){
            criteria.andCreateTimeLike("%"+dto.getCreateTime()+"%");
        }
        if(StringUtil.isNotBlank(dto.getUpdateTime())){
            criteria.andUpdateTimeLike("%"+dto.getUpdateTime()+"%");
        }
        if(StringUtil.isNotBlank(dto.getPublishTime())){
            criteria.andPublishTimeLike("%"+dto.getPublishTime()+"%");
        }
        if(StringUtil.isNotBlank(dto.getDeadlineTime())){
            criteria.andDeadlineTimeLike("%"+dto.getDeadlineTime()+"%");
        }
        if(StringUtil.isNotBlank(dto.getCreatePerson())){
            criteria.andCreatePersonLike("%"+dto.getCreatePerson()+"%");
        }
                
    }

}
